package com.yixing.tech.common.utils;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SerializerUtil {

    private static JsonMapper jsonMapper = new JsonMapper();
    static {
        jsonMapper.configure(MapperFeature.USE_STD_BEAN_NAMING, true);
    }

    public static String writeAsJSONString(Object obj) {
        String result = "";
        try {
            result = jsonMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            log.error(e.getMessage());
        }

        return result;
    }

    public static<T> T readFromJSONString(String jsonStr, Class<T> className) {
        T obj = JSON.parseObject(jsonStr, className);
        return obj;
    }

}
